/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. A copy of the
 * WTFPL version 2 is provided in the source archive, at
 * example/WTFPL-2 .*/
#include <fakegui.h>

static FGSdlSurface* make_main_screen(void) {
  FGSdlSurface* result = fg_sdl_surface_new(60, 25);
  F437Surface* surface = f437_surface_new(fg_sdl_surface_get_surface(result));
  f437_surface_put_rect(surface, 0, 0, 60, 25, 177,
                        F437_COLOR_BWHITE, F437_COLOR_BLACK);
  for (int i = 0 ; i < 5 ; i++) {
    f437_surface_put_rect(surface, 11 + i, 4 - i, 42 - 2 * i, 1, ' ',
                          F437_COLOR_BLACK, F437_COLOR_BBLUE);
    f437_surface_put_rect(surface, 11 + i, 4 + i, 42 - 2 * i, 1, ' ',
                          F437_COLOR_BLACK, F437_COLOR_BBLUE);
  }
  f437_surface_put_string(surface, 15, 4, "    Test Example:  Mock of ZZT    ",
                          F437_COLOR_BWHITE, F437_COLOR_BLUE);
  f437_surface_free(surface);
  return result;
}

static FGPanel* make_side_panel(void) {
  FGPanel* panel = fg_panel_new(20, 25);
  FGTheme* back_theme = fg_theme_new();
  back_theme->panel = (FGPanelTheme){0, F437_COLOR_BLACK, F437_COLOR_BLUE};
  fg_object_set_theme(FG_OBJECT(panel), back_theme);
  back_theme = NULL;

  FGLabel* zzt = fg_label_new("    NOT ZZT    ");
  FGTheme* header_theme = fg_theme_new();
  header_theme->label = (FGLabelTheme){F437_COLOR_BLACK, F437_COLOR_WHITE};
  fg_object_set_theme(FG_OBJECT(zzt), header_theme);
  header_theme = NULL;
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(zzt), FG_COORD(2, 1));
  zzt = NULL;

  FGLabel* pick = fg_label_new("No commands work:");
  FGTheme* blue_text = fg_theme_new();
  blue_text->label = (FGLabelTheme){F437_COLOR_BCYAN, F437_COLOR_BLUE};
  fg_object_set_theme(FG_OBJECT(pick), blue_text);
  blue_text = NULL;
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(pick), FG_COORD(2, 5));
  pick = NULL;

  FGTheme* white_text = fg_theme_new();
  white_text->label = (FGLabelTheme){F437_COLOR_BWHITE, F437_COLOR_BLUE};
  FGLabel* white_text_proto = g_object_ref_sink(fg_label_new(""));
  fg_object_set_theme(FG_OBJECT(white_text_proto), white_text);
  white_text = NULL;
  FGLabel* dashes1 = fg_label_clone(white_text_proto);
  fg_label_set_label(dashes1, "- - - - -");
  FGLabel* dashes2 = fg_label_clone(dashes1);
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(dashes1), FG_COORD(5, 0));
  dashes1 = NULL;
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(dashes2), FG_COORD(5, 2));
  dashes2 = NULL;
  FGLabel* TOWN = fg_label_clone(white_text_proto);
  fg_label_set_label(TOWN, "TOWN");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(TOWN), FG_COORD(9, 8));
  TOWN = NULL;
  FGLabel* play = fg_label_clone(white_text_proto);
  fg_label_set_label(play, "Play");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(play), FG_COORD(6, 11));
  play = NULL;
  FGLabel* about = fg_label_clone(white_text_proto);
  fg_label_set_label(about, "About ZZT!");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(about), FG_COORD(6, 16));
  about = NULL;
  FGLabel* arrow = fg_label_clone(white_text_proto);
  fg_label_set_label(arrow, "\037");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(arrow), FG_COORD(11, 21));
  g_object_unref(white_text_proto);
  white_text_proto = NULL;

  FGTheme* yellow_text = fg_theme_new();
  yellow_text->label = (FGLabelTheme){F437_COLOR_BYELLOW, F437_COLOR_BLUE};
  FGLabel* yellow_text_proto = g_object_ref_sink(fg_label_new(""));
  fg_object_set_theme(FG_OBJECT(yellow_text_proto), yellow_text);
  yellow_text = NULL;
  FGLabel* world = fg_label_clone(yellow_text_proto);
  fg_label_set_label(world, "World:");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(world), FG_COORD(6, 7));
  world = NULL;
  FGLabel* restore = fg_label_clone(yellow_text_proto);
  fg_label_set_label(restore, "Restore game");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(restore), FG_COORD(6, 12));
  restore = NULL;
  FGLabel* quit = fg_label_clone(yellow_text_proto);
  fg_label_set_label(quit, "Quit");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(quit), FG_COORD(6, 13));
  quit = NULL;
  FGLabel* high = fg_label_clone(yellow_text_proto);
  fg_label_set_label(high, "High Scores");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(high), FG_COORD(6, 17));
  high = NULL;
  FGLabel* editor = fg_label_clone(yellow_text_proto);
  fg_label_set_label(editor, "Board Editor");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(editor), FG_COORD(6, 18));
  editor = NULL;
  FGLabel* speed = fg_label_clone(yellow_text_proto);
  fg_label_set_label(speed, "Game Speed:");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(speed), FG_COORD(6, 20));
  speed = NULL;
  FGLabel* scale = fg_label_clone(yellow_text_proto);
  fg_label_set_label(scale, "F....:....S");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(scale), FG_COORD(6, 22));
  scale = NULL;
  g_object_unref(yellow_text_proto);
  yellow_text_proto = NULL;

  FGTheme* cyan_key = fg_theme_new();
  cyan_key->label = (FGLabelTheme){F437_COLOR_BLACK, F437_COLOR_CYAN};
  FGLabel* cyan_proto = g_object_ref_sink(fg_label_new(""));
  fg_object_set_theme(FG_OBJECT(cyan_proto), cyan_key);
  cyan_key = NULL;
  FGLabel* W = fg_label_clone(cyan_proto);
  fg_label_set_label(W, " W ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(W), FG_COORD(2, 7));
  W = NULL;
  FGLabel* R = fg_label_clone(cyan_proto);
  fg_label_set_label(R, " R ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(R), FG_COORD(2, 12));
  R = NULL;
  FGLabel* A = fg_label_clone(cyan_proto);
  fg_label_set_label(A, " A ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(A), FG_COORD(2, 16));
  A = NULL;
  FGLabel* E = fg_label_clone(cyan_proto);
  fg_label_set_label(E, " E ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(E), FG_COORD(2, 18));
  E = NULL;
  g_object_unref(cyan_proto);
  cyan_proto = NULL;

  FGTheme* white_key = fg_theme_new();
  white_key->label = (FGLabelTheme){F437_COLOR_BLACK, F437_COLOR_WHITE};
  FGLabel* white_key_proto = fg_label_new("");
  fg_object_set_theme(FG_OBJECT(white_key_proto), white_key);
  white_key = NULL;
  FGLabel* P = fg_label_clone(white_key_proto);
  fg_label_set_label(P, " P ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(P), FG_COORD(2, 11));
  P = NULL;
  FGLabel* Q = fg_label_clone(white_key_proto);
  fg_label_set_label(Q, " Q ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(Q), FG_COORD(2, 13));
  Q = NULL;
  FGLabel* H = fg_label_clone(white_key_proto);
  fg_label_set_label(H, " H ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(H), FG_COORD(2, 17));
  H = NULL;
  FGLabel* S = fg_label_clone(white_key_proto);
  fg_label_set_label(S, " S ");
  fg_container_add(FG_CONTAINER(panel), FG_OBJECT(S), FG_COORD(2, 20));
  S = NULL;
  g_object_unref(white_key_proto);
  white_key_proto = NULL;

  return panel;
}

int main(void) {
  SDL_Surface* screen;
  SDL_Event ev;
  fg_init();
  SDL_Init(SDL_INIT_VIDEO);
  screen = SDL_SetVideoMode(F437_DEFAULT_WIDTH_PX,
                            F437_DEFAULT_HEIGHT_PX, 0, 0);
  F437Surface* surface = f437_surface_new(screen);
  FGContext* context = fg_context_new(surface);
  fg_container_add(FG_CONTAINER(context),
                   FG_OBJECT(make_main_screen()),
                   FG_COORD(0, 0));
  fg_container_add(FG_CONTAINER(context),
                   FG_OBJECT(make_side_panel()),
                   FG_COORD(60, 0));
  gboolean done = FALSE;
  while (!done) {
    while (SDL_PollEvent(&ev)) {
      if ((ev.type == SDL_KEYDOWN) &&
          (ev.key.keysym.sym == SDLK_ESCAPE)) done = TRUE;
    }
    fg_context_draw(context);
    SDL_Flip(screen);
  }
  SDL_Quit();
  g_object_unref(context);
  f437_surface_free(surface);
  return 0;
}
